﻿
IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_File_DeleteFile_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_File_DeleteFile_V2];
GO

CREATE PROCEDURE [dbo].[sproc_File_DeleteFile_V2]
    @ownerId    int,
    @fileId     int
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;

IF ( @@TRANCOUNT = 0 )
BEGIN
    RAISERROR(N'This sproc must be executed within an active transaction.', 16, 1);
    RETURN -1;
END

DELETE
        f
    FROM dbo.UDS_File_File f INNER JOIN dbo.UDS_File_Directory d ON f.DirectoryId = d.DirectoryId
    WHERE f.FileId = @fileId AND d.OwnerId = @ownerId;
IF ( @@ROWCOUNT <> 1 )
BEGIN
    RAISERROR(N'Cannot delete the given file since it does not exist.', 16, 1);
    RETURN -2;
END

RETURN 0;
END
GO

